import { defineStore } from 'pinia'
import { onMounted, reactive, ref } from 'vue'
import { get, post } from '@/utils/api'
import { MessagePlugin } from 'tdesign-vue-next'

export const useAdminEmailStore = defineStore('adminEmail', () => {
  const saveConfig = async () => {
    try {
      loading.value = true
      const response = await post('/api/email/saveConfig', emailData)
      loading.value = false
      MessagePlugin.closeAll()
      MessagePlugin(response.status, response.message)
      await getConfig()
    } catch (err) {
      console.log(err)
    }
  }

  const getConfig = async () => {
    try {
      const response = await get('/api/email/getConfig')
      response.port = response.port.toString()
      Object.assign(emailData, response)
    } catch (err) {
      console.log(err)
    }
  }

  const emailData = reactive({
    id: 1,
    host: '',
    port: '',
    username: '',
    password: ''
  })

  const loading = ref<boolean>(false)
  const contentLoading = ref<boolean>(true)

  return {
    emailData,
    getConfig,
    saveConfig,
    loading,
    contentLoading
  }
})
